package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.pojo.TbSpecGroup;
import com.leyou.item.pojo.TbSpecParam;
import com.leyou.item.service.TbSpecGroupService;
import com.leyou.item.service.TbSpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: guojy
 * @date: 2020/3/4 16:12
 * @Description: TODO 规格参数操作
 * @version: 1.0
 */
@RestController
@RequestMapping("/spec")
public class SpecController {
    @Autowired
    private TbSpecGroupService tbSpecGroupService;

    @Autowired
    private TbSpecParamService tbSpecParamService;

    /**
     * 根据 分类id，查询对应的规格参数分组
     *
     * @param cateoryId
     * @return
     */
    // api.leyou.com/api/item/spec/groups/of/category/?id=4
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> findSpecGroupByCategoryId(
            @RequestParam(name = "id") Long cateoryId) {
        List<SpecGroupDTO> list = tbSpecGroupService.findSpecGroupByCategoryId(cateoryId);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据组id、分类id、搜索标识，查询对应的组下规格参数
     *
     * @param groupId 组id
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> findSpecParam(
            @RequestParam(name = "gid", required = false) Long groupId,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "searching", required = false) Boolean searching) {
        List<SpecParamDTO> list = tbSpecParamService.findSpecParam(groupId, cid, searching);
        return ResponseEntity.ok(list);
    }
}
